<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: AddComment.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\comment;

use app\api\model\dao\Comment;
use app\library\Error;
use app\library\Ip;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class AddComment extends PageBase
{

    private $objComment;

    public function __construct()
    {
        parent::__construct();

        $this->objComment = new Comment();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'goods_id' => 'require',
            'comment_rank' => 'require',
            'content' => 'require',
            'parent_id' => 'require',
        ];
        $msg = [
            'goods_id.id_value' => '商品ID必填',
            'comment_rank.id_value' => '评价星级必填',
            'content.id_value' => '评价内容必填',
            'parent_id.id_value' => '评价父节点必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $input = [];
        $input['user_id'] = $this->user_id;
        $input['comment_type'] = 0;
        $input['id_value'] = $param['goods_id'];
        $input['content'] = $param['content'];
        $input['parent_id'] = $param['parent_id'];
        $input['comment_rank'] = $param['comment_rank'];
        $input['ip_address'] = Ip::getClientIp();
        $input['user_name'] = $this->user_name;
        $input['add_time'] = time();
        $ret = $this->objComment->addComment($input);
        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
